package com.aurora.source.home.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.aurora.source.R;
import com.aurora.source.home.bean.ContactUsDataBean;
import com.aurora.source.util.ErrorImgUtil;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author: pipi
 * @description:检疫检测
 * @date: 2018/9/7
 * @version: 1.0
 */
public class DetectionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private boolean upData = true;
    private List<ContactUsDataBean.CompanyQuarantineDTOListBean> datas = new ArrayList<>();
    private static final int TYPE_ADD = 1;

    public List<ContactUsDataBean.CompanyQuarantineDTOListBean> getDatas() {
        return datas;
    }

    private static final int TYPE_COUNT = 2;
    private Context context;
    int widths;
    int heights;
    private OnEditClickListener onEditClickListener; //编辑
    private OnAddImgClickListener onAddImgClickListener; //添加图片
    private OnImgClickListener onImgClickListener; //修改图片
    private OnDeleteClickListener onDeleteClickListener;//删除数据

    public void setOnImgClickListener(OnImgClickListener onImgClickListener) {
        this.onImgClickListener = onImgClickListener;
    }

    public void setOnDeleteClickListener(OnDeleteClickListener onDeleteClickListener) {
        this.onDeleteClickListener = onDeleteClickListener;
    }

    public void setDatas(List<ContactUsDataBean.CompanyQuarantineDTOListBean> companyHonorDTOList) {
        datas.clear();
        datas.addAll(companyHonorDTOList);
        notifyDataSetChanged();
    }

    public interface OnAddImgClickListener {
        void onAddImgClick();
    }
    public void setOnAddImgClickListener(OnAddImgClickListener onAddImgClickListener) {
        this.onAddImgClickListener = onAddImgClickListener;
    }
    @Override
    public int getItemViewType(int position) {
        if (position == datas.size()) {
            return TYPE_ADD;
        } else {
            return TYPE_COUNT;
        }
    }

    @Override
    public int getItemCount() {
        if(upData){
            return datas.size() + 1;
        }else {
            return datas.size();
        }
    }

    public DetectionAdapter(Context context, boolean updateCompany){
        this.context = context;
        DisplayMetrics metric = new DisplayMetrics();
        WindowManager wn = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wn.getDefaultDisplay().getMetrics(metric);
        widths = metric.widthPixels;     // 屏幕宽度（像素）
        heights = metric.heightPixels;   // 屏幕高度（像素）
        upData = updateCompany;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder;
        if (viewType == TYPE_ADD) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_add_comp_img, parent, false);
            holder = new AddImgHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_qualification, parent, false);
            holder = new MyHolder(view);
        }
        return holder;
    }

    public class AddImgHolder extends RecyclerView.ViewHolder {

        public AddImgHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        if (getItemViewType(position) == TYPE_ADD) {  //添加图片
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onAddImgClickListener != null) {
                        onAddImgClickListener.onAddImgClick();
                    }
                }
            });
        } else {
            final MyHolder imageHolder = (MyHolder) holder;
            ViewGroup.LayoutParams layoutParams = imageHolder.ivImage.getLayoutParams();
            layoutParams.width = widths / 2;
            imageHolder.ivImage.setLayoutParams(layoutParams);
            Glide.with(context)
                    .load(datas.get(position).getCompanyQuarantinePath())
                    .apply(new RequestOptions().placeholder(R.drawable.ic_default).centerCrop().error(ErrorImgUtil.getErrorImgRandom()))
                    .into(imageHolder.ivImage);
            imageHolder.tvTitle.setText(datas.get(position).getImageTitle()+"");

            imageHolder.ivImage.setOnClickListener(new View.OnClickListener() { //修改图片
                @Override
                public void onClick(View v) {
                    if(onImgClickListener!=null){
                        onImgClickListener.onImgClick(datas.get(position),position);
                    }
                }
            });

            imageHolder.ivDelete.setOnClickListener(new View.OnClickListener() { //删除图片
                @Override
                public void onClick(View v) {
                    if(onDeleteClickListener!=null){
                        onDeleteClickListener.onDeleteClick(datas.get(position),position);
                    }
                }
            });

            imageHolder.ivUpdate.setOnClickListener(new View.OnClickListener() { //更新图片
                @Override
                public void onClick(View v) {
                    if(onEditClickListener!=null){
                        onEditClickListener.onEditClick(datas.get(position),position);
                    }
                }
            });

            if(upData){
                imageHolder.ivDelete.setVisibility(View.VISIBLE);
                imageHolder.ivUpdate.setVisibility(View.VISIBLE);
            }else {
                imageHolder.ivDelete.setVisibility(View.GONE);
                imageHolder.ivUpdate.setVisibility(View.GONE);
            }
        }
    }



    public void setOnEditClickListener(OnEditClickListener onEditClickListener) {
        this.onEditClickListener = onEditClickListener;
    }

    public interface OnEditClickListener{
        void onEditClick(ContactUsDataBean.CompanyQuarantineDTOListBean dtoListBean,int position);
    }

    public interface OnImgClickListener{
        void onImgClick(ContactUsDataBean.CompanyQuarantineDTOListBean dtoListBean,int position);
    }

    public interface OnDeleteClickListener{
        void onDeleteClick(ContactUsDataBean.CompanyQuarantineDTOListBean dtoListBean,int position);
    }

    public class MyHolder extends RecyclerView.ViewHolder {

        Context context;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.iv_image)
        ImageView ivImage;
        @BindView(R.id.iv_update)
        ImageView ivUpdate;
        @BindView(R.id.iv_delete)
        ImageView ivDelete;
        public MyHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }

    }
}
